package com.hamizhong.iflowers.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import com.hamizhong.iflowers.service.ICityService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.service.IProvinceService;
import com.hamizhong.iflowers.web.base.MyBaseAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.CartGoodsVO;
import com.hamizhong.iflowers.web.vo.GoodsVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.ProvinceVO;

public class InitOrderAction extends MyBaseAction {

	private IGoodsService iGoodsService = null;
	private IProvinceService iProvinceService = null;
	private ICityService iCityService = null;
	
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	public IProvinceService getiProvinceService() {
		return iProvinceService;
	}
	public void setiProvinceService(IProvinceService iProvinceService) {
		this.iProvinceService = iProvinceService;
	}
	public ICityService getiCityService() {
		return iCityService;
	}
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}
	@Override
	protected String doRequest() {
		logger.info("----------------- Hello InitOrderAction -----------------");
		String cartGoodsStr = "";
		String newCartGoodsStr = "";
		String[] arr0;//长度为2，用于暂时存放key和value
		String[] arr1;//购物车中已经添加的个数（用“_”分隔）
		String[] arr2;//存放商品的key
		String[] arr3;//存放商品的数量
		//key和value之间通过“-”分隔
		
		GoodsVO goodsVO = null;
		CartGoodsVO cartGoodsVO = null;
		
		try {
			IOPResultDto<IObjectVO> dtoProvince = iProvinceService.findAll();
			if(dtoProvince.isSuccess()) {
				List<ProvinceVO> provinceVOList = dtoProvince.getFirstResult().getProvinceVOList();
				getRQ().setAttribute("provinceVOList", provinceVOList);
			}
			
			Cookie[] cookie = getRQ().getCookies();
			if(cookie != null) {
				for(int i=0;i<cookie.length;i++) {
					String res = cookie[i].getName();
					if(res.equals("iFlowers_Shopping_Cart")) {
						cartGoodsStr = cookie[i].getValue();
						break;
					}
				}
			}
			if(cartGoodsStr != "") {
				arr1 = cartGoodsStr.split("_");
				arr2 = new String[arr1.length];
				arr3 = new String[arr1.length];

				for(int i=0;i<arr1.length;i++) {
					arr0 = new String[2];
					arr0 = arr1[i].split("-");
					arr2[i] = arr0[0];
					arr3[i] = arr0[1];
				}
				
				List<CartGoodsVO> cartGoodsVOList = new ArrayList<CartGoodsVO>();
				for(int i=0;i<arr2.length;i++) {
					goodsVO = new GoodsVO();
					IOPResultDto<IObjectVO> dto = iGoodsService.findById(Integer.valueOf(arr2[i]));
					goodsVO = dto.getFirstResult().getGoodsVOList().get(0);
					cartGoodsVO = new CartGoodsVO();
					cartGoodsVO.setGoodsId(goodsVO.getGoodsId());
					cartGoodsVO.setGoodsName(goodsVO.getGoodsName());
					cartGoodsVO.setGoodsPrice(goodsVO.getGoodsPrice());
					cartGoodsVO.setState(goodsVO.getState());
					cartGoodsVO.setGoodsCnt(Integer.valueOf(arr3[i]));
					cartGoodsVO.setGoodsPictureS(goodsVO.getGoodsPictureS());
					cartGoodsVOList.add(cartGoodsVO);
				}
				getRQ().setAttribute("cartGoodsVOList", cartGoodsVOList);
				return SUCCESS;
			}else {
				return "noCartGoods";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
